package cn.lee.wechat.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.lee.wechat.util.DateUtils;
import cn.zhouyafeng.itchat4j.api.MessageTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 发送消息
 * @author hui
 *
 */
@Api(value="WeChatMessageController")
@RestController
@RequestMapping(value="/message")
public class WeChatMessageController {
	/**
	 * Logger
	 */
	private static final  Logger logger = LogManager.getLogger(WeChatMessageController.class.getName());
	
	@Value("${user.nikename}")
	private String[]  userNikeName;
	
	@Value("${user.test}")
	private String testUser;
	
	
	/**
	 * test send a message to Mr.Lee
	 * @param message message context 
	 * @return
	 */
	@ApiResponses({
		@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
		@ApiResponse(code=400,message="请求参数没填好")
	})
	@ApiOperation(value="发送测试消息，测试发送消息接口是否正常",notes="发送测试消息，测试发送消息接口是否正常，如果返回true则说明正常")
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public String testSendMessage(){
		MessageTools.sendMsgByNickName("Test send Message ,Now time is :"+DateUtils.getNowTime(), testUser);
		logger.debug("send test message ok");
		return Boolean.TRUE.toString();
	}
	
	/**
	 * send message to someone
	 * @param message message context 
	 * @return
	 */
	@ApiResponses({
		@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
		@ApiResponse(code=400,message="请求参数没填好")
	})
	@ApiOperation(value="给用户发送消息",notes="给用户发送给消息，具体发送给谁看系统配置application.yml中user.nikename")
	@ApiImplicitParam(name="message",value="发送消息的内容",required = true, dataType = "String")
	@RequestMapping(value="/send",method=RequestMethod.POST)
	public String sendMessage(String message){
		for (String nikename : userNikeName) {
			MessageTools.sendMsgByNickName(message, nikename);
			logger.debug("send message to "+nikename +"Ok ");
		}
		return Boolean.TRUE.toString();
	}
}
